<?
class Log extends DataObject
{
	public $_dbTable = 'logs';
    public $_dbIndex = 'logID';
    protected $_arValidStatus = array('started','aborted','error','finished');
	
	public $_arFields = array('status',
							  'module',
                              'task',
                              'target',
                              'start_comment',
                              'end_comment',
                              'userID',
							  'date_created',
							  'date_modified');	
    /*
	$opts = userID, target, start_comment, status
	*/
	public function __construct($dbLink,$module, $task,$opts = array())
    {
		parent::__construct();
		$this->setDBLink($dbLink);
		$this->module = $module;
		$this->task = $task;
		foreach($opts as $var => $value)
		{
			$this->$var = $value;
		}
		
		$this->date_created = date("Y-m-d H:i:s");
		if($opts['status'] && in_array($status,$this->_arValidStatus))
			$this->status = $opts['status'];
		else
			$this->status = "finished";
		if($this->status == "finished")
		{
			$this->date_modified = date("Y-m-d H:i:s");
		}
		$this->create();
	}
	public function setStatus($status = "finished", $end_comment = "")
	{
		if(!in_array($status,$this->_arValidStatus)) return 0;
		$this->status = $status;
		$this->date_modified = date("Y-m-d H:i:s");
		return $this->update();
	}
}
?>